package controllers;

import com.fasterxml.jackson.databind.JsonNode;
import dao.Ship_Repository;
import play.data.FormFactory;
import play.libs.Json;
import play.libs.concurrent.HttpExecutionContext;
import play.mvc.Controller;
import play.mvc.Result;

import javax.inject.Inject;
import java.util.concurrent.CompletionStage;

public class ShipController extends Controller {
    private final FormFactory formFactory;
    private final Ship_Repository ship_repository;
    private final HttpExecutionContext ec;

    @Inject
    public ShipController(FormFactory formFactory, Ship_Repository ship_repository, HttpExecutionContext ec) {
        this.formFactory = formFactory;
        this.ship_repository = ship_repository;
        this.ec = ec;
    }

    public CompletionStage<Result> getShipInfo(){
        JsonNode json = request().body().asJson();

        return ship_repository.getShipInfo().thenApplyAsync(_GetListByProc -> {
            return ok(Json.toJson(_GetListByProc));
        }, ec.current());
    }

    public CompletionStage<Result> getBerthingSchedule(){
        JsonNode json = request().body().asJson();

        return ship_repository.getBerthingSchedule().thenApplyAsync(_GetListByProc -> {
            return ok(Json.toJson(_GetListByProc));
        }, ec.current());
    }

    public CompletionStage<Result> updateBerthSKD() {
        JsonNode json = request().body().asJson();
        String BerthOrders = json.findPath("BerthOrders").textValue();
        String ApplyIDs = json.findPath("ApplyIDs").textValue();
        String Operator = json.findPath("Operator").textValue();
        String OperateFlag = json.findPath("OperateFlag").textValue();


        return ship_repository.updateBerthSKD(BerthOrders, ApplyIDs, Operator, OperateFlag).thenApplyAsync(_updateBerth -> {
            return ok(Json.toJson(_updateBerth));
        }, ec.current());
    }
}
